$(function () {
    $("#sendbtn").click(function () {
        var phone = $("#addphone").val();
        if (phone == "" || phone == null) {
            alert("手机号不能为空");
            return;
        }
        //启动计时器
        isStop = setInterval("tine()", 1000);
        //禁用发送按钮
        $("#sendbtn").attr("disabled", true);
        //发送验证码
        $.getJSON("/blogger/isphone.do", {phone: phone}, function (result) {
            if (result.msg == "ok") {
                alert("验证码发送成功");
            } else {
                alert("验证码发送失败");
                $("#sendbtn").html("再次发送验证码");
                $("#sendbtn").removeAttr("disabled");
                sec = 60;
                clearInterval(isStop);
            }
        });
    });

    $("#addBtn").click(function () {
        var code = $("#code").val();
        var adduserName = $("#adduserName").val();
        var addpassword = $("#addpassword").val();
        var addphone = $("#addphone").val();
        if (adduserName == null || adduserName == "") {
            alert("用户名不能为空！");
            return;
        }
        if (addpassword == null || addpassword == "") {
            alert("密码不能为空！");
            return;
        }
        if (addphone == null || addphone == "") {
            alert("手机号不能为空！");
            return;
        }
        if (code == null || code == "") {
            alert("验证码不能为空！");
            return;
        } else {
            if (codeFlag) {
                $.getJSON("/blogger/isyes.do", {code: code}, function (result) {
                    if (result.msg == "ok") {
                        $("#sendbtn").html("验证成功");
                        $("#sendbtn").attr("disabled", true);
                        clearInterval(isStop);
                        codeFlag = true;
                    } else {
                        clearInterval(isStop);
                        $("#sendbtn").html("验证失败");
                        return;
                    }
                });
            }
        }
        if (!codeFlag) {
            $.getJSON("/blogger/add.do", {
                adduserName: adduserName,
                addpassword: addpassword,
                addphone: addphone
            }, function (result) {
                if (result.msg == "ok") {
                    alert("注册成功！请登录");
                    window.location.href = "/jsp/login.jsp";
                } else {
                    alert("注册失败！");
                    $("#adduserName").val(result.blogg.userName);
                    $("#addpassword").val(result.blogg.password);
                    $("#addphone").val(result.blogg.mobile);
                    return;
                }
            });
        }


    });
});
var codeFlag = false;
var sec = 60;
var isStop = 0;

function tine() {
    sec--;
    if (sec == 0) {
        $("#sendbtn").html("再次发送验证码");
        $("#sendbtn").removeAttr("disabled");
        sec = 60;
        clearInterval(isStop);
    } else {
        $("#sendbtn").html(sec + "s后重新发送");
    }
}